<?php

class Act_item extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->library('session');
		$this->load->library('form_validation');
		$this->load->library('encrypt');
		$this->load->library('enc');
		$this->load->model('mmaster');
        $this->load->model('mglobal');
		$this->load->model('mitem');
	}
	
	public function getById( $id ) {
		if( isset( $id ) )
			echo json_encode( $this->mitem->getById( $id ) );
	}
	
	public function create() {
		if( !empty( $_POST ) ) {
			echo $this->mitem->create();
			//echo 'New user created successfully!';
		}
	}
	
	public function read() {
		//echo json_encode( $this->mitem->getAll() );

		$result = $this->mitem->getAll();
		//encode
        for($i=0;$i<count($result);$i++){
    		$result[$i]->item_code = $this->enc->fogit($result[$i]->item_code);
    	}
        
        echo json_encode($result);
	}

	function defoger()
    {
        $result = $this->enc->defog($this->input->post('str'));
        echo $result;  
    }
	
	public function update() {
		if( !empty( $_POST ) ) {
			$item_group_id=$_POST['item_group_id'];
			$dbr_id=$_POST['dbr_id'];
			$item_category_id=$_POST['item_category_id'];
            $default_price=$_POST['default_price'];
			
			//$item_id = $_POST['itid'];
			//$item_code = $this->mitem->getItemCode($item_id);

			$this->mitem->update($item_group_id,$dbr_id,$item_category_id,$default_price);
			//echo 'Record updated successfully!';
			//redirect('/item/itemmanagement');
			
			
			$name = $this->mitem->getItemGroupName($item_group_id);
			$data['groupname'] = $name['item_group_name'];
			$data['message'] = 'Record updated successfully!';
			$data['updated'] = 'Updated!';
			echo json_encode ($data);
			
		}
	}
	
	public function updateItemGroupID($id){
		$item_id = $id;
		$item_group_id=$_POST['item_group_id'];
		$this->mitem->updateGroupID($item_id,$item_group_id);
	
	}
	
		
	public function delete( $id = null ) {
		if( is_null( $id ) ) {
			echo 'ERROR: Id not provided.';
			return;
		}
		$this->mitem->delete( $id );
		echo 'Records deleted successfully';
	}
} //end class
